<template>
  <div class="dictionaries"></div>
</template>

<script>
import { getTSType, getTSTypeTable } from "@/api/dma/dictionaries/index";
export default {
  name: "dictionaries",
  props: {
    searchData: {
      type: Array,
      required: true,
    },
    modelType: {
      type: String,
      required: true,
    },
    newData: {},
  },
  data() {
    return {
      dictionaryData: {
        dictionaryObj: {},
        // 字典分组编码
        typeGroupCode: "",
      },
      // 转换后的options数据
      optionData: [],
      // option的假数据
      option: [],
    };
  },
  mounted() {
    if (this.searchData) {
      const searchDataT = JSON.stringify(this.searchData);
      if (searchDataT) {
        this.dictionaryData.dictionaryObj = JSON.parse(
          JSON.stringify(this.searchData)
        );
      }
    }
  },
  methods: {
    // 字典表查询   |    跨表查询
    // 字典表查询
    getTypeGroupCode() {
      return new Promise(async (resolve, reject) => {
        let searchData = this.searchData;
        // 设置一个空对象
        var Obj = {};
        for (let i = 0; i < searchData.length; i++) {
          // 遍历后获取字典编码
          let typeGroupCode = searchData[i].typeGroupCode;

          // 遍历后获得每一个的EN值
          if (typeGroupCode) {
            let index = i;
            let EN = searchData[index].EN;
            const res = await getTSType(typeGroupCode);
            let optionDatas = res.data;
            // 转换value,label  ---------------
            optionDatas.forEach((item) => {
              for (var key in item) {
                if (key === "typeCode") {
                  item["value"] = item[key];
                  delete item[key];
                }
                if (key === "typeName") {
                  item["label"] = item[key];
                  delete item[key];
                }
              }
            });
            Obj[EN] = optionDatas;
            searchData[i].option = optionDatas;
          }
        }
        resolve(Obj);
      });
    },
    // 跨表查询
    getTypeGroupCodeAcross() {
      return new Promise(async (resolve, reject) => {
        let searchData = this.searchData;
        let objAcross = {};
        for (let i = 0; i < searchData.length; i++) {
          if (searchData[i].dictTable) {
            let table = searchData[i].dictTable.table;
            let id = searchData[i].dictTable.id;
            let name = searchData[i].dictTable.name;
            let associated = searchData[i].dictTable.associated;
            if (!associated) {
              associated = "";
            }
            let index = i;
            let EN = searchData[index].EN;
            console.log(table, id, name, associated);

            const res = await getTSTypeTable(table, id, name, associated);
            searchData[i].option = optionDatas;
            let optionDatas = res.data;
            objAcross[EN] = optionDatas;
          }
        }
        resolve(objAcross);
      });
    },
    // 传递optionData
    sendOptionData(option) {
      this.$emit("optionData", option);
    },
  },
  async created() {
    console.log(5555);
    const result = await Promise.all(
      [this.getTypeGroupCode(), this.getTypeGroupCodeAcross()].map((p) =>
        p.catch((e) => console.log(e))
      )
    );
    this.sendOptionData(result);
  },
};
</script>

<style>
</style>
